package pl.rgolcz.antnet;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import org.apache.commons.collections15.Factory;

import edu.uci.ics.jung.algorithms.generators.random.EppsteinPowerLawGenerator;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseMultigraph;

public class RandomGraphGenerator
{
    public static void main(String[] args) throws IOException
    {
        Graph<Integer,IWeightedEdge> graph = generateGraph();
        FileWriter fileWriter = new FileWriter(new File("random.net"));
        GraphWriter writer = new GraphWriter(fileWriter);
        writer.write(graph);
    }
    
    static Graph<Integer,IWeightedEdge> generateGraph() 
    {
        Graph<Integer,IWeightedEdge> g =
            new EppsteinPowerLawGenerator<Integer,IWeightedEdge>(
                    new GraphFactory(), new VertexFactory(), new EdgeFactory(), 26, 50, 50).create();
        Set<Integer> removeMe = new HashSet<Integer>();
        for (Integer v : g.getVertices()) {
            if ( g.degree(v) == 0 ) {
                removeMe.add( v );
            }
        }
        for(Integer v : removeMe) {
            g.removeVertex(v);
        }
        return g;
    }
    
    static class GraphFactory implements Factory<Graph<Integer,IWeightedEdge>> 
    {
        public Graph<Integer,IWeightedEdge> create() 
        {
            return new SparseMultigraph<Integer,IWeightedEdge>();
        }
    }
    
    static class VertexFactory implements Factory<Integer> 
    {
        int current = 0;
        public Integer create() 
        {
            return current++;
        }
        
    }
    
    static class EdgeFactory implements Factory<IWeightedEdge> 
    {
        Random rnd = new Random(System.currentTimeMillis());
        public IWeightedEdge create() 
        {
            return new WeightedEdge(rnd.nextDouble());
        }
        
    }
}
